package com.cdks.wuxiu.api.controller;

import com.cdks.wuxiu.api.component.constant.Constant;
import com.cdks.wuxiu.api.model.common.AuthParam;
import com.cdks.wuxiu.api.model.dto.UserInfoDTO;
import com.cdks.wuxiu.api.model.param.WalletGetBillParam;
import com.cdks.wuxiu.api.model.vo.WalletHomeVO;
import com.cdks.wuxiu.api.service.UserService;
import com.cdks.wuxiu.api.service.WalletService;
import com.cdks.wuxiu.api.util.TimeUtils;
import com.cdks.wuxiu.common.model.api.ApiResult;
import com.cdks.wuxiu.entity.mysql.UserBillInfo;
import com.cdks.wuxiu.entity.mysql.UserWalletInfo;

import cn.hutool.json.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;


@Api(tags = "钱包")
@Slf4j
@RestController
@RequestMapping("/wallet")
public class WalletController {
    @Resource
    private WalletService walletService;
    @Resource
    private UserService userService;
    

    @ApiOperation("用户钱包首页")
    @PostMapping("/home")
    public ApiResult<WalletHomeVO> home(@RequestBody @Valid AuthParam param) {       
        if (param.getUserInfo().getType() == Constant.ACCOUNT_TYPE_KEFU)
        {
            return ApiResult.failed(10, "无权限");
        }

        UserInfoDTO user  = userService.getUserByIdWithBalance(param.getUserId(), TimeUtils.getTimeSecond());
        int bean  = user.getBean();
        int coin  = user.getCoin();
        if (user.getAuthLevel() > 0)
        {
            coin  = coin + user.getScore();
        }
        else
        {
            bean  = bean + user.getScore();
        }

        WalletHomeVO vo  = new WalletHomeVO();
        vo.setBean(bean);
        vo.setCoin(coin);
        vo.setBeanHint("https://wuxiu.cdks.net/web/wallet/beanHint.html");
        vo.setCoinHint("https://wuxiu.cdks.net/web/wallet/coinHint.html");
        vo.setBillInfo(walletService.getBill(param.getUserId()));

        return ApiResult.success(vo);
    }

    @ApiOperation("获取余额")
    @PostMapping("/getBalance")
    public ApiResult<WalletHomeVO> getBalance(@RequestBody @Valid AuthParam param) {
        WalletHomeVO vo  = new WalletHomeVO();
        if (param.getUserInfo().getType() > Constant.ACCOUNT_TYPE_TEST)
        {
            vo.setBean(10000);
        }
        else
        {
            UserWalletInfo wallet  = walletService.getUserWalletNoScore(param.getUserId());
            if (wallet == null)
            {
                return ApiResult.failed(10, "账号信息有误");
            }

            vo.setBean(wallet.getBean());
            vo.setCoin(wallet.getCoin());
        }

        return ApiResult.success(vo);
    }

    @ApiOperation("获取收支明细")
    @PostMapping("/getBill")
    public ApiResult<Object> getBill(@RequestBody @Valid WalletGetBillParam param) {
        List<UserBillInfo> billList  = walletService.getBill(param.getUserId(), param.getPage(), param.getStart(), param.getStop());
        
        JSONObject json  = new JSONObject();
        json.set("billList", billList);

        return ApiResult.success(json);
    }
}
